package com.wyg.blog.controller.manage_blog;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.model.blog.Comment;
import com.wyg.blog.service.blog.BlogService;
import com.wyg.blog.service.blog.CommentService;
import com.wyg.blog.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/2 22:30
 */
@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    BlogService blogService;

    @GetMapping("/getSomeComments")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<List<Map<String,String>>> getSomeComments(@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        Page<Comment> page = new Page<>(current,size);
        IPage<Comment> commentIPage = commentService.selectSomeComments(page);
        List<Map<String,String>> results = new ArrayList<>();
        List<Comment> comments = commentIPage.getRecords();
        long total = commentIPage.getTotal();
        long MyCurrent = commentIPage.getCurrent();
        long MySize = commentIPage.getSize();
        for(int i = 0; i < comments.size(); i++){
            Map<String,String> result = new HashMap<>();
            Comment comment = comments.get(i);
            result.put("id", comment.getId().toString());
            String blogName = blogService.selectTitleById(comment.getBlog_id());
            result.put("blog_name", blogName);
            result.put("create_time", comment.getCreate_time().toString());
            result.put("author", comment.getAuthor());
            result.put("email", comment.getEmail());
            result.put("state", "" + comment.isState());
            if (comment.getParent_id() == null){
                result.put("parent_id", null);
            } else {
                result.put("parent_id", comment.getParent_id().toString());
            }
            result.put("reply", comment.getReply());
            result.put("total", "" + total);
            result.put("current", "" + MyCurrent);
            result.put("size", "" + MySize);
            results.add(result);
        }
        return new JsonResult<>(results, "200", "评论查询成功!");
    }

    @PostMapping("/changeCommentState")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> changeCommentState(@RequestBody Map<String, Integer> id){
        boolean state = commentService.selectState(id.get("id"));
        boolean res = commentService.renewCommentState(id.get("id"), !state);
        if (res) {
            return new JsonResult<>(1,"200","评论状态修改成功");
        } else {
            return new JsonResult<>(0, "1002", "评论状态修改失败!");
        }
    }

    @GetMapping("/getContentById")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<String> getContentById(Integer id){
        String content = commentService.selectContentById(id);
        return new JsonResult<>(content,"200","内容查询成功!");
    }

    @GetMapping("/deleteComment")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN','SUPER_ADMIN')")
    public JsonResult<Integer> deleteComment(Integer id) {
        boolean res = commentService.removeComment(id);
        if (res) {
            return new JsonResult<>(1, "200", "评论删除成功!");
        } else {
            return new JsonResult<>(0, "1001","评论删除失败!");
        }
    }
}
